package com.raytoo.module.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.raytoo.common.persistence.dao.MenuMapper;
import com.raytoo.common.persistence.model.Menu;
import com.raytoo.module.sys.dao.MenuDao;
import com.raytoo.module.sys.service.IMenuService;

/**
 * 菜单服务
 */
@Service
public class MenuServiceImpl implements IMenuService {
	
	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private MenuDao menuDao;
	
	@Override
    public void delMenu(Integer menuId) {

        //删除菜单
        this.menuMapper.deleteById(menuId);

        //删除关联的r_role_menu
        this.menuDao.deleteRoleMenuByMenuId(menuId);
    }
	
	@Override
	public void delMenuContainSubMenus(Integer menuId) {
		Menu menu = menuMapper.selectById(menuId);
		
		//删除当前菜单
        delMenu(menuId);
        
      //删除所有子菜单
        Wrapper<Menu> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pcodes", "%[" + menu.getCode() + "]%");
        List<Menu> menus = menuMapper.selectList(wrapper);
        for (Menu temp : menus) {
            delMenu(temp.getId());
        }
	}


}
